using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using GameUI;
using UnityEngine.Events;

public class MinimapIconObjectController:MonoBehaviour {
	public Sprite mapIconSprite;
	public Sprite minimapIconSprite;

	[SerializeField] bool syncWithFow;
	[SerializeField] UnityEvent onClick;

	public int mapIconId { get; protected set; }
	public int minimapIconId { get; protected set; }
	public bool overrideMapIconDeactivate;
	public bool overrideMinimapIconDeactivate;

	private void Start() {
		if(mapIconSprite)
			mapIconId=MinimapCameraController.instance.AddMapIcon(
			mapIconSprite,transform.position,() => onClick?.Invoke());
		if(minimapIconSprite)
			minimapIconId=MinimapCameraController.instance.AddMinimapIcon(
			minimapIconSprite,transform.position);
	}
	private void OnDestroy() {
		MinimapCameraController.instance.RemoveMapIcon(mapIconId);
		MinimapCameraController.instance.RemoveMinimapIcon(minimapIconId);
	}

	void Update() {
		bool explored = TilemapController.ExploredAt(transform.position);
		if(!syncWithFow) explored=true;
		if(minimapIconSprite) {
			MinimapCameraController.instance.SetMinimapIconPosition(minimapIconId,transform.position);
			MinimapCameraController.instance.SetMinimapIconActive(minimapIconId,explored&&!overrideMinimapIconDeactivate);
			MinimapCameraController.instance.SetMinimapIconSprite(minimapIconId,minimapIconSprite);
		}
		if(mapIconSprite) {
			MinimapCameraController.instance.SetMapIconPosition(mapIconId,transform.position);
			MinimapCameraController.instance.SetMapIconActive(mapIconId,explored&&!overrideMapIconDeactivate);
			MinimapCameraController.instance.SetMapIconSprite(mapIconId,mapIconSprite);
		}

	}
}
